<?php
class BdPushComponent
{
    const PUSH_TYPE_ARTICLE = 10;// article
    const PUSH_TYPE_ARTICLE_LIST = 11;
    const PUSH_TYPE_PRODUCT = 20;// for product
    const PUSH_TYPE_PRODUCT_LIST = 21;
    const PUSH_TYPE_TICKET = 30;
    const PUSH_TYPE_ORDER = 40;
    const PUSH_TYPE_BALANCE = 50;
    const PUSH_TYPE_VERSION = 70;
    public $error = array();

    /**
     * [__construct description]
     * @param [type] $apiKey    [description]
     * @param [type] $secretKey [description]
     * openssl pkcs12 -in MyApnsCert.p12 -out MyApnsCert.pem -nodes
     */
    public function __construct($apiKey=null, $secretKey=null)
    {
        Yii::import('ext.BaiduPush.Channel');
        $this->apiKey = is_null($apiKey) ? 'SlunkYDIX0oZMDPRR2WOeE1B' : $apiKey;
        $this->secretKey = is_null($secretKey) ? '3V9iObmzUnPemDrd1OrsAHjM8nZqPUvD' : $secretKey;
        $this->channel = new Channel($this->apiKey, $this->secretKey, array(CURLOPT_SSL_VERIFYHOST=>0));
        // $this->channel->setCurlOpts(array(CURLOPT_SSL_VERIFYHOST=>0));
        // $channel->setHost(Channel::HOST_IOS_DEV);
    }

    public function initAppIoscert()
    {
        // $fd = fopen($devcert, 'r');
        // $devcert = fread($fd, filesize($devcert)); // 开发版APNs pem证书
        // $fd = fopen($discert, 'r');
        // $discert = fread($fd, filesize($discert)); // 发布版APNs pem证书

        // $ret = $this->channel->initAppIoscert($cert_name, $cert_des, $dis_cert, $dev_cert); // cert_name和cert_des您自定义字符串即可
        // if (false === $ret) {
        //     error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!!' ) ;
        //     error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        //     error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        //     error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
        // }  else {
        //     right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        //     right_output ( 'result: ' . print_r ( $ret, true ) ) ;
        // }
    }

    /**
     * [test description]
     * @param  [type] $title   [description]
     * @param  [type] $content [description]
     * @return [type]          [description]
     * title
     * description
     * notification_builder_id:0
     * notification_basic_style:7
     * "open_type":0,
     * "net_support" : 1,
     * "user_confirm": 0,
     * "url": "http://developer.baidu.com",
     * "pkg_content":"",
     * "pkg_name" : "com.baidu.bccsclient",
     * "pkg_version":"0.1",
     */
    public function pushArticle($params, $intent=array())
    {
        $result = false;
        $push_type = Channel::PUSH_TO_ALL;
        $optional[Channel::MESSAGE_TYPE] = 1;
        $intent['_service'] = self::PUSH_TYPE_ARTICLE;
        $message['custom_content'] = $intent;
        $message['description'] = $params['content'];
        $message['title'] = $params['title'];
        $message['net_support'] = 1;
        $message_key = array('title');
        $ret = $this->channel->pushMessage($push_type, $message, $message_key, $optional);
        if (false === $ret) {
            $this->channel->errno();
            $this->channel->errmsg();
            $this->channel->getRequestId();
        } else {
            Yii::log(json_encode($ret), 'push', CLogger::LEVEL_INFO);
            $result = true;
        }
        return $result;
    }

    public function pushArticleList()
    {}

    public function pushProduct($params, $intent=array())
    {
        $result = false;
        $push_type = Channel::PUSH_TO_ALL;

        // build ios params
        $optional_ios = array();
        $message_ios = array();
        $optional_ios[Channel::MESSAGE_TYPE] = 1;
        $optional_ios[Channel::DEPLOY_STATUS] = 1;
        $optional_ios[Channel::DEVICE_TYPE] = 4;//ios
        $message_ios['title'] = '赚吧有新商品了';
        $message_ios['description'] = $params['title'];
        $message_ios['aps'] = array();
        $message_ios['aps']['alert'] = $params['title'];
        $message_ios['type'] = self::PUSH_TYPE_PRODUCT;
        $message_ios['id'] = $intent['product_id'];
        // $message_ios['aps']['badge'] = 3;
        $message_ios['aps']['sound'] = '';

        // build android params
        $optional_android = array();
        $message_android = array();
        $optional_android[Channel::MESSAGE_TYPE] = 1;
        $optional_android[Channel::DEVICE_TYPE] = 3;//android
        $intent['_service'] = self::PUSH_TYPE_PRODUCT;
        $message_android['custom_content'] = $intent;
        $message_android['description'] = $params['content'];
        $message_android['title'] = $params['title'];
        $message_android['net_support'] = 1;

        // use same key
        $message_key = array('title');

        // push query
        $ret = $this->channel->pushMessage($push_type, $message_ios, $message_key, $optional_ios);
        $ret = $this->channel->pushMessage($push_type, $message_android, $message_key, $optional_android);
        if (false === $ret) {
            $this->channel->errno();
            $this->channel->errmsg();
            $this->channel->getRequestId();
        } else {
            Yii::log(json_encode($ret), 'push', CLogger::LEVEL_INFO);
            $result = true;
        }
        return $result;
    }

    public function pushProductList($params)
    {
        $result = false;
        $push_type = Channel::PUSH_TO_ALL;
        $optional[Channel::MESSAGE_TYPE] = 1;
        $intent['_service'] = self::PUSH_TYPE_PRODUCT_LIST;
        $message['custom_content'] = $intent;
        $message['description'] = $params['content'];
        $message['title'] = $params['title'];
        $message['net_support'] = 1;
        $message_key = array('title');
        $ret = $this->channel->pushMessage($push_type, $message, $message_key, $optional);
        if (false === $ret) {
            $this->channel->errno();
            $this->channel->errmsg();
            $this->channel->getRequestId();
        } else {
            Yii::log(json_encode($ret), 'push', CLogger::LEVEL_INFO);
            $result = true;
        }
        return $result;
    }

    public function pushOrder($params, $user, $intent=array())
    {
        $push_type = 1;
        $optional = array();
        $optional[Channel::MESSAGE_TYPE] = 0;
        $optional[Channel::USER_ID] = $user;
        $intent['_service'] = self::PUSH_TYPE_ORDER;
        $message['custom_content'] = $intent;
        $message['description'] = $params['content'];
        $message['title'] = $params['title'];
        $message['net_support'] = 1;
        $message_key = array('title');
        $ret = $this->channel->pushMessage($push_type, $message, $message_key, $optional);
        var_dump($ret);
    }

    public function pushOrderList()
    {}

    public function pushVersion($params, $intent=array())
    {
        $result = false;
        $push_type = Channel::PUSH_TO_ALL;
        $optional[Channel::MESSAGE_TYPE] = 1;
        $intent['_service'] = self::PUSH_TYPE_VERSION;
        $message['custom_content'] = $intent;
        $message['description'] = $params['content'];
        $message['title'] = $params['title'];
        $message['net_support'] = 1;
        $message_key = array('title');
        $ret = $this->channel->pushMessage($push_type, $message, $message_key, $optional);
        if (false === $ret) {
            $this->channel->errno();
            $this->channel->errmsg();
            $this->channel->getRequestId();
        } else {
            Yii::log(json_encode($ret), 'push', CLogger::LEVEL_INFO);
            $result = true;
        }
        return $result;
    }
}
?>
